package edu.cuit.avatar.demo;

import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class PrintCalendarDemo {
    public static void main(String[] args) {
        Calendar calendar = new GregorianCalendar();
        int today = calendar.get(Calendar.DAY_OF_MONTH);
        int month = calendar.get(Calendar.MONTH);//从0开始
        //设置这个月的第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        //得到这一天为星期几
        int weekday = calendar.get(Calendar.DAY_OF_WEEK);
        //得到星期的起始日
        int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);
        
        int indent = 0;
        while(weekday != firstDayOfWeek) {
            indent++;
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            weekday = calendar.get(Calendar.DAY_OF_WEEK);
        }
        String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
        do {
            System.out.printf("%5s",weekdayNames[weekday]);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            weekday = calendar.get(Calendar.DAY_OF_WEEK);
        }while(weekday != firstDayOfWeek);
        System.out.println();
        for(int i = 1; i <= indent; i++) {
            System.out.print("    ");
        }
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        do {
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            System.out.printf("%4d",day);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            if(day == today)System.out.print("*  ");
            else System.out.print("   ");
            weekday = calendar.get(Calendar.DAY_OF_WEEK);
            if(weekday == firstDayOfWeek) System.out.println();
        }while(calendar.get(Calendar.MONTH) == month);
    }
}